package com.rightsidetech.http.controller.admin.system;

import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.enums.ModifyName;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.ModifyLog;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.SysUpdateStatusReq;
import com.rightsidetech.system.dtos.request.agent.SysAgentInfoAddReq;
import com.rightsidetech.system.dtos.request.agent.SysAgentInfoReq;
import com.rightsidetech.system.dtos.request.agent.SysAgentInfoUpdateReq;
import com.rightsidetech.system.dtos.response.agent.SysAgentInfoResp;
import com.rightsidetech.system.service.SysAgentInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

/**
 * @Author: yhr
 * @Date: 2021-10-25 14:12
 */
@RestController
@RequestMapping("/admin/sys_AgentInfo")
@ShenyuSpringMvcClient(path = "/admin/sys_AgentInfo/**")
@Api(tags = "admin:代理商帐号相关")
public class SysAgentInfoController extends BaseController {

    @Resource
    private SysAgentInfoService sysAgentInfoService;

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("账号列表")
    @GetMapping("/page")
    public ResponseData<PageInfo<SysAgentInfoResp>> selectPage(SysAgentInfoReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysAgentInfoService.page(req,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("添加帐号")
    @PostMapping("/saveAgentInfo")
    @ModifyLog(serviceClass = SysAgentInfoService.class,modifyType = ModifyName.SAVE)
    public ResponseData saveAgentInfo(@RequestBody SysAgentInfoAddReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysAgentInfoService.saveAgentInfo(req,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("编辑帐号")
    @PostMapping("/updateAgentInfo")
    @ModifyLog(serviceClass = SysAgentInfoService.class)
    public ResponseData updateAgentInfo(@RequestBody SysAgentInfoUpdateReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysAgentInfoService.updateAgentInfo(req,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("删除帐号")
    @PostMapping("/del")
    @ModifyLog(serviceClass = SysAgentInfoService.class)
    public ResponseData updateAgentInfo(@RequestBody DelReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return sysAgentInfoService.delAgentInfo(req);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("编辑帐号状态")
    @PostMapping("/updateStatus")
    @ModifyLog(serviceClass = SysAgentInfoService.class)
    public ResponseData updateAgentInfoStatus(@RequestBody SysUpdateStatusReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return sysAgentInfoService.updateAgentInfoStatus(req);
    }

    @Log
    @Token
    @ApiOperation("代理商姓名下拉")
    @GetMapping("/search")
    public ResponseData selectList() throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysAgentInfoService.selectList(loginInfoId);
    }

}
